package com.drodds.game;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class GameEngine {
	/*
	 * Slot Machine absolute size and position
	 */
	private static float absSlotHtoWRatio = 0.34f;
	private static float absSlotWidth = 1.0f;
	private static float absSlotX = 0.0f;
	private static float absSlotY = 1.0f;
	
	private int values_count = AllValues.values().length;
	private int num_reels = 3;
	private int num_stops = 10;
	private int num_values = values_count;
	private Rect slotBounds;
	private Rect btnDockRect;
	private Handler uiHandler;
	private boolean lastSlotState = false;
	private SlotMachine PlayerSlots;
	
	public void init(Context ctxt, Handler h) {
		PlayerSlots = new SlotMachine(num_reels, num_stops, num_values);
		PlayerSlots.Init(ctxt.getResources());
		
		uiHandler = h;
		
    	Message dockMsg = uiHandler.obtainMessage();
    	Bundle bundle = new Bundle();
    	bundle.putSerializable("message", AllUIMessages.GET_DOCK_POSITION);
    	dockMsg.setData(bundle);
    	uiHandler.sendMessage(dockMsg);
	}
	
	public void update(float deltaTime) {
		PlayerSlots.update(deltaTime);
		
		if (!PlayerSlots.spinning && lastSlotState) {
	    	Message dockMsg = uiHandler.obtainMessage();
	    	Bundle bundle = new Bundle();
	    	bundle.putSerializable("message", AllUIMessages.REELS_STOPPED);
	    	dockMsg.setData(bundle);
	    	uiHandler.sendMessage(dockMsg);
		}
		lastSlotState = PlayerSlots.spinning;
		
		Message resultsMsg = uiHandler.obtainMessage();
		Bundle resultsBundle = new Bundle();
		resultsBundle.putSerializable("message", AllUIMessages.RESULTS_READY);
		resultsBundle.putInt("result", PlayerSlots.GetResult());
		resultsBundle.putInt("resultRange", PlayerSlots.GetResultRange());
		resultsMsg.setData(resultsBundle);
		uiHandler.sendMessage(resultsMsg);
		
    	Message dockMsg = uiHandler.obtainMessage();
    	Bundle bundle = new Bundle();
    	bundle.putSerializable("message", AllUIMessages.GET_DOCK_POSITION);
    	dockMsg.setData(bundle);
    	uiHandler.sendMessage(dockMsg);
    	
	}
	
	public void draw(Canvas c) {
    	Rect screenRect = getDrawableRect(c.getWidth(), c.getHeight());

    	int slotL = screenRect.left + Math.round(screenRect.width() * absSlotX);
    	int slotR = slotL + Math.round(screenRect.width() * absSlotWidth);
    	int slotHeight = Math.round((slotR - slotL) * absSlotHtoWRatio);
    	int slotT = Math.round(screenRect.height() - (slotHeight * absSlotY));
    	int slotB = slotT + slotHeight;
    	
    	slotBounds = new Rect(slotL, slotT, slotR, slotB);
    	PlayerSlots.draw(c, slotBounds);
	}
	
    public void SpinReels() {
    	PlayerSlots.Spin();
    }
    
    public Rect getDrawableRect(int w, int h) {
    	Rect rtnRect = new Rect();
  	
    	if (btnDockRect.top == 0) {
			rtnRect.bottom = h;
			
    		//dock is at the left or right of the screen
    		if (btnDockRect.bottom == h) {
        		rtnRect.top = 0;
    			//dock is on the left
    			if (btnDockRect.left == 0) {
	    			rtnRect.left = btnDockRect.right;
	    			rtnRect.right = w;
	    		}
    			//dock is on the right
	    		else {
	    			rtnRect.left = 0;
	    			rtnRect.right = btnDockRect.left;
	    		}
    		}
    		//dock is at the top of the screen
    		else {
    			rtnRect.top = btnDockRect.bottom;
    			rtnRect.left = 0;
    			rtnRect.right = w;
    		}
    	}
    	//dock is at the bottom of the screen
    	else {
    		rtnRect.top = 0;
    		rtnRect.left = 0;
    		rtnRect.right = w;
    		rtnRect.bottom = btnDockRect.top;
    	}
 
    	return rtnRect;
    }
    
    public void setDockBounds(Rect r) {
    	btnDockRect = r;
    }
}
